package com.wthink.hzgj.copyFile

import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.R
import com.wthink.hzgj.copyFile.adapter.CameraListAdapter
import com.wthink.hzgj.copyFile.adapter.CameraListCheckAdapter
import com.wthink.hzgj.copyFile.callback.DirectoryIsEmpty
import com.wthink.hzgj.copyFile.viewmodel.CopyFileDirectoryViewModel
import com.wthink.hzgj.copyFile.viewmodel.CopyProgressDialogModel
import com.wthink.hzgj.data.domain.FileDetail
import com.wthink.hzgj.databinding.FragmentCopyFileDirectoryBinding
import com.wthink.hzgj.util.`interface`.NoticeCallBack
import com.wthink.hzgj.util.`interface`.ProgrossCallBack
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.hzgj.util.setBackgroundAlpha
import com.wthink.hzgj.util.toast
import kotlinx.android.synthetic.main.item_copy_file.view.*

class CopyFileDirectoryFragment : Fragment(){

    private val viewModel by viewModels<CopyFileDirectoryViewModel>()

    private lateinit var binding: FragmentCopyFileDirectoryBinding

    var pathList : ArrayList<String> = ArrayList()

    private val copyProgressDialogModel by viewModels<CopyProgressDialogModel>()

    private val copyProgressDialog by lazy {
        CopyProgressDialog.make(
            BusApplication.busContext, binding.root as ViewGroup, copyProgressDialogModel,
            activity as AppCompatActivity
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_copy_file_directory,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }

        viewModel.start()

        viewModel.setprogressCallBack(object : ProgrossCallBack{
            override fun progress(pro: String) {
                if (pro.equals("100")) {
                    copyProgressDialog.dismiss()
                }
            }
        })


        binding.dirListView.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)

        val bundle = arguments?.getString("path")

        if (bundle != null) {
            viewModel.queryDetail(bundle)
        }

        binding.dirListView.adapter = object : CameraListCheckAdapter(){}.apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {

                    val fileDetail = obj as FileDetail
                    viewModel.setdirectoryIsEmpty(object : DirectoryIsEmpty {
                        override fun fileSize(size: Int) {
                            if (size > 0) {
                                val args = Bundle()
                                args.putString("path",fileDetail.path)
                                Navigation.findNavController(binding.dirListView).navigate(R.id.copyFileVideoFragment,args)
                            }
                        }
                    })

                    viewModel.queryDetailCheck(fileDetail.path)

                   /* val fileDetail = obj as FileDetail
                    viewModel.queryDetailCheck(fileDetail.path).let {
                        if (it.isEmpty() || it.size == 0) {
                            return
                        }
                        val args = Bundle()
                        args.putString("path",fileDetail.path)
                        Navigation.findNavController(binding.dirListView).navigate(R.id.copyFileVideoFragment,args)
                    }*/
                }
            }
        }

        binding.copy.setOnClickListener {
            pathList.clear()
            val list = viewModel.getList()
            for (fileDetail in list) {
                if (fileDetail.isChecked) {
                    pathList.add(fileDetail.path)
                }
            }

            if (pathList.size == 0) {
                toast(BusApplication.busContext,"未选中视频")
                return@setOnClickListener
            }

            viewModel.copyFile(pathList)
            copyProgressDialog.showAtLocation(
                binding.root,
                Gravity.CENTER,
                0,
                0
            )
        }
        closeDefaultAnimator(binding.dirListView)
    }
}